package com.hmis.hmis.UserInterface.publishUI;

import com.hmis.dao.*;
import com.hmis.domain.Bed;
import com.hmis.domain.Doctor;
import com.hmis.domain.Patient;
import com.hmis.domain.Ward;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;

import java.util.List;

//登记模块
public class Publish {

    private static Publish publish;
    Label title = new Label("登记病人");
    TextField inpn = new TextField();
    TextField insex = new TextField();
    ComboBox<String> combodep = new ComboBox<>();
    ComboBox<String> combodn = new ComboBox<>();
    ComboBox<Long> combobed = new ComboBox<>();
    ComboBox<Long> combown = new ComboBox<>();
    TextArea disease = new TextArea();
    TextArea patientInfo = new TextArea();

    Label tip1 = new Label("");
    Button refresh = new Button("重置");
    Button commit = new Button("提交");
    private ClinicDAO clinicDAO = ClinicDAO.clinicDAO;
    private PatientDAO patientDAO = PatientDAO.patientDAO;
    private DoctorDAO doctorDAO = DoctorDAO.doctorDAO;
    private WardDAO wardDAO = WardDAO.wardDAO;
    private BedDAO bedDAO = BedDAO.bedDAO;
    private StackPane main = new StackPane();
    private VBox vBox = new VBox(15);

    private Publish() {
        Label op1 = new Label("");
        op1.setPrefHeight(5);
        title.setPrefHeight(30);
        title.setStyle("-fx-font: 25 arial");

        HBox hbnames = new HBox(200);
        hbnames.setAlignment(Pos.CENTER);

        HBox hbPatient = new HBox(30);
        Label lbpn = new Label("患者姓名：");
        inpn.setPromptText("请输入患者姓名");
        inpn.setPrefWidth(150);
        hbPatient.getChildren().addAll(lbpn, inpn);

        HBox hbSex = new HBox(30);
        Label lbsex = new Label("性别:");
        insex.setPrefWidth(100);
        hbSex.getChildren().addAll(lbsex, insex);

        HBox hbdep = new HBox(30);
        Label lbdep = new Label("挂诊科室：");
        combodep.prefWidth(100);
        List<Ward> wardList = wardDAO.listWards();
        for (Ward ward : wardList) {
            combodep.getItems().add(ward.getDeptName());
        }
        hbdep.getChildren().addAll(lbdep, combodep);
        hbnames.getChildren().addAll(hbPatient, hbSex, hbdep);


        HBox hbnos = new HBox(200);
        hbnos.setAlignment(Pos.CENTER);

        HBox hbDoctor = new HBox(30);
        Label lbdn = new Label("医生姓名：");
        combodn.prefWidth(150);
        List<Doctor> doctorList = doctorDAO.listDoctor();
        for (Doctor doctor : doctorList) {
            combodn.getItems().add(doctor.getDoctorName());
        }

        hbDoctor.getChildren().addAll(lbdn, combodn);

        //为部门与医生名字提供联系监控
        combodep.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                combodn.getItems().clear();
                for (Doctor doctor : doctorList) {
                    String dept = doctor.getDeptName();
                    if (dept.equals(newValue)) {
                        combodn.getItems().add(doctor.getDoctorName());
                    }
                }
            }
        });

//        combodn.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
//            @Override
//            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
//                if(combodep.getSelectionModel().getSelectedItem()!=null) {
//                    return;
//                }
//                combodep.getItems().clear();
//                for(Doctor doctor:doctorList) {
//                    String doctorName = doctor.getDoctorName();
//                    if(doctorName.equals(newValue)) {
//                        combodep.getItems().add(doctor.getDeptName());
//                    }
//                }
//            }
//        });

        HBox hbward = new HBox(30);
        Label lbwn = new Label("病房号：");
        combown.prefWidth(150);
        for (Ward ward : wardList) {
            combown.getItems().add(ward.getWardNo());
        }
        hbward.getChildren().addAll(lbwn, combown);

        HBox hbbed = new HBox(30);
        Label lbbedn = new Label("病床号：");
        combobed.prefWidth(150);
        List<Bed> bedList = bedDAO.listBed();

        //监控病房号下拉框与病床号下拉框的联系
        combown.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Long>() {
            @Override
            public void changed(ObservableValue<? extends Long> observable, Long oldValue, Long newValue) {
                if(combown.getSelectionModel().getSelectedItem()==null) {
                    return;
                }
                combobed.getItems().clear();
                for (Bed bed : bedList) {
                    long wardNo = combown.getSelectionModel().getSelectedItem();
                    int status = bed.getStatus();
                    if (bed.getWardNo() == wardNo && status == 0) {
                        combobed.getItems().add(bed.getTheBedNo());
                    }
                }
            }
        });

        hbbed.getChildren().addAll(lbbedn, combobed);

        hbnos.getChildren().addAll(hbDoctor, hbward, hbbed);

        disease.setPromptText("请填写病情");
        disease.setPrefHeight(200);

        patientInfo.clear();
        patientInfo.setPromptText("登记信息");
        patientInfo.setPrefHeight(200);

        //不允许编辑
        patientInfo.setEditable(false);

        tip1.setPrefHeight(20);
        tip1.setAlignment(Pos.CENTER);
        tip1.setStyle("-fx-font: 15 arial;-fx-text-fill:red");

        HBox hbbts = new HBox(50);
        hbbts.setAlignment(Pos.CENTER);
        refresh.setPrefSize(50, 25);
        commit.setPrefSize(50, 25);
        hbbts.getChildren().addAll(refresh, commit);

        Label op2 = new Label("");
        op2.setPrefHeight(10);

        vBox.setAlignment(Pos.CENTER);
        vBox.getChildren().addAll(op1, title, hbnames, hbnos, disease,patientInfo, tip1, hbbts, op2);
        vBox.prefWidthProperty().bind(main.maxWidthProperty().multiply(1));
        main.getChildren().addAll(vBox);
//        main.setPrefWidth(1075);


        refresh.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                refreshUI();
            }
        });

        commit.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                tip1.setText("");
                String patientName = inpn.getText();
                String sex = insex.getText();

                String doctorName = combodn.getSelectionModel().getSelectedItem();
                Doctor doctor = doctorDAO.findByName(doctorName);

                String deptName = combodep.getSelectionModel().getSelectedItem();
                Long wardNo = combown.getSelectionModel().getSelectedItem();

                Long thebedNo = combobed.getSelectionModel().getSelectedItem();
                Bed bed = bedDAO.findByWardNoAndTheBedNo(wardNo, thebedNo);

                String illness = disease.getText();

                if (patientName == "" || sex == "" || doctor == null || deptName == "" || bed == null || illness == "") {
                    tip1.setText("表单存在空值，请仔细填写再提交");
                    return;
                }
                tip1.setText("");

                Long doctorId = doctor.getDoctorId();
                Long deptId = doctor.getDeptId();
                Long bedNo = bed.getBedNo();

                Patient patient = new Patient();
                patient.setPatientName(patientName);
                patient.setSex(sex);
                patient.setWardNo(wardNo);
                patient.setBedNo(bedNo);
                patient.setTheBedNo(thebedNo);
                patient.setDeptId(deptId);
                patient.setDeptName(deptName);
                patient.setDoctorId(doctorId);
                patient.setDoctorName(doctorName);
                patient.setIllness(illness);
                patient.setDiagnose("");
                patient.setStatus(0);
                patient.setIsDelete(0);
                patientDAO.appendPatient(patient);

                bed.setStatus(1);
                bedDAO.bedService.updateById(bed);

                refreshUI();
                tip1.setText("登记成功");
                patientInfo.clear();
                patientInfo.setText(patientDAO.getPatientInfo(patient));

            }
        });


    }

    public static Publish getPublish() {
        if (publish == null) {
            publish = new Publish();
        }
        return publish;
    }

    public void refreshUI() {

        inpn.setText("");
        insex.setText("");
        disease.setText("");
        combodn.getSelectionModel().clearSelection();
        combodep.getSelectionModel().clearSelection();
        combown.getSelectionModel().clearSelection();
        combobed.getSelectionModel().clearSelection();
        combodn.getItems().clear();
        tip1.setText("");
        patientInfo.clear();
    }

    public StackPane getMain() {
        return main;
    }
}
